package org.example.learn.javaagent.apply_demo.track;

import java.util.Stack;

public class TrackManager {

    private static final ThreadLocal<Stack<Span>> track = new ThreadLocal<Stack<Span>>();

   private static Span createSpan(){
        Stack<Span> stack = track.get();
        if (null == stack) {
            stack = new Stack<>();
            track.set(stack);
        }
        String linkId;
        if (stack.isEmpty()) {
            linkId = TrackContext.getLinkId();
            if (null == linkId){
                linkId = "nvl";
                TrackContext.setLinkId(linkId);
            }
        }else {
            Span span = stack.peek();
            linkId = span.getLinkId();
            TrackContext.setLinkId(linkId);
        }
        return new Span(linkId);
   }

    public static Span createEntrySpan(){
        Span span = createSpan();
        Stack<Span> stack = track.get();
        stack.push(span);
        return span;
    }

    public static Span getExitSpan(){
        Stack<Span> stack = track.get();
        if (null == stack || stack.isEmpty()) {
            TrackContext.clear();
            return null;
        }
        return stack.pop();
    }

    public static Span getCurrentSpan(){
        Stack<Span> stack = track.get();
        if (null == stack || stack.isEmpty()) {
            return null;
        }
        return stack.peek();
    }
}
